<?php
namespace app\app\controller\v1;
use app\app\Appbase;
use think\facade\Session;
use think\facade\Cache;
use auth\Api;

//
//购物车
//
class Cart extends Appbase 
{
	
	/**
	* 获取用户购物车
	* /app/v1/Cart/getUserInfo
	* 是否需要登录 
	* @is_need_login true
	*/
	public function getUserCartList($give_type=1){
		try {
			$cartList = model('ShopCart')->getCartList(request() ->userId,$give_type);
			$nums = [
				"cart_num"=> count($cartList),
				"goods_num"=> array_sum(array_column($cartList, 'cart_num')),
			];
			return $this->ReSucess( compact('cartList','nums') );	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	* 添加购物车
	* /app/v1/Cart/add
	* 是否需要登录 
	* @is_need_login true
	* @method post
	*/
	public function setCart($goods_id=0,$num=1,$specs_id=0,$seckill_id=0,$type=1,$is_new=0){
		if(!$num){
			return $this->ReError("数量必填！");
		}
		if(!$goods_id){
			return $this->ReError("商品id必填！");
		}
	
		$uid = request() ->userId;
		
		try {
			
			$goods = model('goods')
				->where('id',$goods_id)
				->where(function($query){
					$query->where('type','in','1,2');
					$query->whereor('is_integral_gift',1);
				})
				->value('id');
				
			if($is_new==0 && $goods ){
				return $this->ReError("该类商品不能加入购物车,只能立即购买！");
			}
				
			$shopCart = model('ShopCart');
			if($type==1){
				$result = $shopCart->setCartInc($uid,$goods_id,$specs_id,$num,$is_new,$seckill_id);
			}else{
				$result = $shopCart->setCartDec($uid,$goods_id,$specs_id,$num,$is_new,$seckill_id);
			}
			return $this->ReSucess($result);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
	/**
	* 更新购物车数量
	* /app/v1/Cart/add
	* 是否需要登录 
	* @is_need_login true
	* @method post
	*/
	public function changeCartNum($cart_id=0,$type=1){
		if(!$cart_id){
			return $this->ReError("购物车id必填！");
		}
		$uid = request() ->userId;
		try {
			$result = model('ShopCart')->changeUserCartNum($uid,$cart_id,$type);
			return $this->ReSucess($result);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	* 删除购物车
	* /app/v1/Cart/removeUserCart
	* 是否需要登录 
	* @is_need_login true
	* @method post
	*/
	public function removeUserCart($cart_id=0){
		if(!$cart_id){
			return $this->ReError("购物车id必填！");
		}
		try {
			$result = model('ShopCart')->removeUserCart(request() ->userId,$cart_id);
			return $this->ReSucess($result);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	* 清空购物车
	* /app/v1/Cart/removeUserCart
	* 是否需要登录 
	* @is_need_login true
	* @method post
	*/
	public function emptyUserCart(){
		$uid = request() ->userId;
		try {
			$ShopCart = model('ShopCart');
			$result = $ShopCart->emptyUserCart($uid);
			if($result === false){
				return $this->ReError( $ShopCart->errorInfo );
			}
			return $this->ReSucess($result);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	* 获取用户购物车数量
	* /app/v1/Cart/add
	* 是否需要登录 
	* @is_need_login true
	* @method post
	*/
	public function getUserCartNum(){
		$uid = request() ->userId;
		try {
			$ShopCart = model('ShopCart');
			$result = $ShopCart->getUserCartNum($uid);
			if($result === false){
				return $this->ReError( $ShopCart->errorInfo );
			}
			return $this->ReSucess($result);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
}



